home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tests / regexp.test < prev    next >
Text File  |  1992-11-06  |  9KB  |  272 lines

  1. # Commands covered:  regexp, regsub
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /user6/ouster/tcl/tests/RCS/regexp.test,v 1.5 91/10/27 15:20:14 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. catch {unset foo}
  21. test regexp-1.1 {basic regexp operation} {
  22.     regexp ab*c abbbc
  23. } 1
  24. test regexp-1.2 {basic regexp operation} {
  25.     regexp ab*c ac
  26. } 1
  27. test regexp-1.3 {basic regexp operation} {
  28.     regexp ab*c ab
  29. } 0
  30.  
  31. test regexp-2.1 {getting substrings back from regexp} {
  32.     set foo {}
  33.     list [regexp ab*c abbbbc foo] $foo
  34. } {1 abbbbc}
  35. test regexp-2.2 {getting substrings back from regexp} {
  36.     set foo {}
  37.     set f2 {}
  38.     list [regexp a(b*)c abbbbc foo f2] $foo $f2
  39. } {1 abbbbc bbbb}
  40. test regexp-2.3 {getting substrings back from regexp} {
  41.     set foo {}
  42.     set f2 {}
  43.     list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
  44. } {1 abbbbc bbbb}
  45. test regexp-2.4 {getting substrings back from regexp} {
  46.     set foo {}
  47.     set f2 {}
  48.     set f3 {}
  49.     list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  50. } {1 abbbbc bbbb c}
  51. test regexp-2.5 {getting substrings back from regexp} {
  52.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  53.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  54.     list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) 12223345556789999 \
  55.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  56.         $f6 $f7 $f8 $f9
  57. } {1 12223345556789999 1 222 33 4 555 6 7 8 9999}
  58. test regexp-2.6 {getting substrings back from regexp} {
  59.     set foo 2; set f2 2; set f3 2; set f4 2
  60.     list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  61. } {1 a a {} {}}
  62. test regexp-2.7 {getting substrings back from regexp} {
  63.     set foo 1; set f2 1; set f3 1; set f4 1
  64.     list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  65. } {1 ac a {} c}
  66.  
  67. test regexp-3.1 {-indices option to regexp} {
  68.     set foo {}
  69.     list [regexp -indices ab*c abbbbc foo] $foo
  70. } {1 {0 5}}
  71. test regexp-3.2 {-indices option to regexp} {
  72.     set foo {}
  73.     set f2 {}
  74.     list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
  75. } {1 {0 5} {1 4}}
  76. test regexp-3.3 {-indices option to regexp} {
  77.     set foo {}
  78.     set f2 {}
  79.     list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
  80. } {1 {0 5} {1 4}}
  81. test regexp-3.4 {-indices option to regexp} {
  82.     set foo {}
  83.     set f2 {}
  84.     set f3 {}
  85.     list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  86. } {1 {0 5} {1 4} {5 5}}
  87. test regexp-3.5 {-indices option to regexp} {
  88.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  89.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  90.     list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
  91.         12223345556789999 \
  92.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  93.         $f6 $f7 $f8 $f9
  94. } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
  95. test regexp-3.6 {getting substrings back from regexp} {
  96.     set foo 2; set f2 2; set f3 2; set f4 2
  97.     list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  98. } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
  99. test regexp-3.7 {getting substrings back from regexp} {
  100.     set foo 1; set f2 1; set f3 1; set f4 1
  101.     list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  102. } {1 {1 2} {1 1} {-1 -1} {2 2}}
  103.  
  104. test regexp-4.1 {-nocase option to regexp} {
  105.     regexp -nocase foo abcFOo
  106. } 1
  107. test regexp-4.2 {-nocase option to regexp} {
  108.     set f1 22
  109.     set f2 33
  110.     set f3 44
  111.     list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
  112. } {1 aBbbxYXxxZ Bbb xYXxx}
  113.  
  114. test regexp-5.1 {exercise cache of compiled expressions} {
  115.     regexp .*a b
  116.     regexp .*b c
  117.     regexp .*c d
  118.     regexp .*d e
  119.     regexp .*e f
  120.     regexp .*a bbba
  121. } 1
  122. test regexp-5.2 {exercise cache of compiled expressions} {
  123.     regexp .*a b
  124.     regexp .*b c
  125.     regexp .*c d
  126.     regexp .*d e
  127.     regexp .*e f
  128.     regexp .*b xxxb
  129. } 1
  130. test regexp-5.3 {exercise cache of compiled expressions} {
  131.     regexp .*a b
  132.     regexp .*b c
  133.     regexp .*c d
  134.     regexp .*d e
  135.     regexp .*e f
  136.     regexp .*c yyyc
  137. } 1
  138. test regexp-5.4 {exercise cache of compiled expressions} {
  139.     regexp .*a b
  140.     regexp .*b c
  141.     regexp .*c d
  142.     regexp .*d e
  143.     regexp .*e f
  144.     regexp .*d 1d
  145. } 1
  146. test regexp-5.5 {exercise cache of compiled expressions} {
  147.     regexp .*a b
  148.     regexp .*b c
  149.     regexp .*c d
  150.     regexp .*d e
  151.     regexp .*e f
  152.     regexp .*e xe
  153. } 1
  154.  
  155. test regexp-6.1 {regexp errors} {
  156.     list [catch {regexp a} msg] $msg
  157. } {1 {wrong # args: should be "regexp ?-nocase? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  158. test regexp-6.2 {regexp errors} {
  159.     list [catch {regexp -nocase a} msg] $msg
  160. } {1 {wrong # args: should be "regexp ?-nocase? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  161. test regexp-6.3 {regexp errors} {
  162.     list [catch {regexp -nocas a} msg] $msg
  163. } {0 0}
  164. test regexp-6.4 {regexp errors} {
  165.     list [catch {regexp a( b} msg] $msg
  166. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  167. test regexp-6.5 {regexp errors} {
  168.     list [catch {regexp a( b} msg] $msg
  169. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  170. test regexp-6.6 {regexp errors} {
  171.     list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
  172. } {1 {too many substring variables}}
  173. test regexp-6.7 {regexp errors} {
  174.     set f1 44
  175.     list [catch {regexp abc abc f1(f2)} msg] $msg
  176. } {1 {couldn't set variable "f1(f2)"}}
  177.  
  178. test regexp-7.1 {basic regsub operation} {
  179.     list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
  180. } {1 xax111aaa222xaa}
  181. test regexp-7.2 {basic regsub operation} {
  182.     list [regsub aa+ aaaxaa &111 foo] $foo
  183. } {1 aaa111xaa}
  184. test regexp-7.3 {basic regsub operation} {
  185.     list [regsub aa+ xaxaaa 111& foo] $foo
  186. } {1 xax111aaa}
  187. test regexp-7.4 {basic regsub operation} {
  188.     list [regsub aa+ aaa 11&2&333 foo] $foo
  189. } {1 11aaa2aaa333}
  190. test regexp-7.5 {basic regsub operation} {
  191.     list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
  192. } {1 xaxaaa2aaa333xaa}
  193. test regexp-7.6 {basic regsub operation} {
  194.     list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
  195. } {1 xax1aaa22aaaxaa}
  196. test regexp-7.7 {basic regsub operation} {
  197.     list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
  198. } {1 xax1aa22aaxaa}
  199. test regexp-7.8 {basic regsub operation} {
  200.     list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
  201. } "1 {xax1\\aa22aaxaa}"
  202. test regexp-7.9 {basic regsub operation} {
  203.     list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
  204. } "1 {xax1\\122aaxaa}"
  205. test regexp-7.10 {basic regsub operation} {
  206.     list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
  207. } "1 {xax1\\aaaaaxaa}"
  208. test regexp-7.11 {basic regsub operation} {
  209.     list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
  210. } {1 xax1&aaxaa}
  211. test regexp-7.12 {basic regsub operation} {
  212.     list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
  213. } {1 xaxaaaaaaaaaaaaaaxaa}
  214. test regexp-7.13 {basic regsub operation} {
  215.     set foo xxx
  216.     list [regsub abc xyz 111 foo] $foo
  217. } {0 xxx}
  218.  
  219. test regexp-8.1 {case conversion in regsub} {
  220.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  221. } {1 xaAAaAAay}
  222. test regexp-8.2 {case conversion in regsub} {
  223.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  224. } {1 xaAAaAAay}
  225. test regexp-8.3 {case conversion in regsub} {
  226.     set foo 123
  227.     list [regsub a(a+) xaAAaAAay & foo] $foo
  228. } {0 123}
  229.  
  230. test regexp-9.1 {-all option to regsub} {
  231.     set foo 86
  232.     list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
  233. } {1 a|xxx|b|xx|c|x|d|x|}
  234. test regexp-9.2 {-all option to regsub} {
  235.     set foo 86
  236.     list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
  237. } {1 a|XxX|b|xx|c|X|d|x|}
  238. test regexp-9.3 {-all option to regsub} {
  239.     set foo 86
  240.     list [regsub x+ axxxbxxcxdx |&| foo] $foo
  241. } {1 a|xxx|bxxcxdx}
  242. test regexp-9.4 {-all option to regsub} {
  243.     set foo 86
  244.     list [regsub -all bc axxxbxxcxdx |&| foo] $foo
  245. } {0 86}
  246. test regexp-9.5 {-all option to regsub} {
  247.     set foo xxx
  248.     list [regsub -all node "node node more" yy foo] $foo
  249. } {1 {yy yy more}}
  250.  
  251. test regexp-10.1 {regsub errors} {
  252.     list [catch {regsub a b c} msg] $msg
  253. } {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
  254. test regexp-10.2 {regsub errors} {
  255.     list [catch {regsub -nocase a b c} msg] $msg
  256. } {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
  257. test regexp-10.3 {regsub errors} {
  258.     list [catch {regsub -nocase -all a b c} msg] $msg
  259. } {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
  260. test regexp-10.4 {regsub errors} {
  261.     list [catch {regsub a b c d e f} msg] $msg
  262. } {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
  263. test regexp-10.5 {regsub errors} {
  264.     list [catch {regsub -nocas a b c} msg] $msg
  265. } {0 0}
  266. test regexp-10.6 {regsub errors} {
  267.     list [catch {regsub -nocase a( b c d} msg] $msg
  268. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  269. test regexp-10.7 {regsub errors} {
  270.     list [catch {regsub -nocase aaa aaa xxx f1(f2)} msg] $msg
  271. } {1 {couldn't set variable "f1(f2)"}}
  272.